using System;
using System.Linq;
using Microsoft.Ajax.Utilities;
using XRM.Security.DataAccess.Enums;
using XRM.Security.DataAccess.Models;
using XRM.Security.WebUI.Helpers;

namespace XRM.Security.WebUI.Models.Template
{
    public class ActSettingSCSIConverter : ITemplateSerializer
    {
        public string Serialize(DataAccess.Models.Request req, RequestWorkKind requestWorkKind = null)
        {
            var scsi = requestWorkKind.RequestWorkKindParams.FirstOrDefault(x => x.SkziKindId.HasValue);
            var entity = new ActSettingSCSIType()
            {
                CreateDate = DateTime.Now.ToShortDateString(),
                SCSIKind = scsi.IfNotNull(x => x.SkziKind.FullName),
                SCSIExemplar = requestWorkKind.RequestWorkKindParams.Where(x => x.WorkKindParam.CustomId == ((int)WorkKindParamCode.CopyNumber)).Select(x => x.ParamValue).FirstOrDefault(),
                SerialNumberSystemUnit = requestWorkKind.RequestWorkKindParams.Where(x => x.WorkKindParam.CustomId == ((int)WorkKindParamCode.SerialNumberAdd)).Select(x => x.ParamValue).FirstOrDefault(),
                User = string.Format("{1} {0} {2}", req.UserRegister.FirstName, req.UserRegister.LastName, req.UserRegister.MotherName),
                SystemUser = string.Format("{1} {0} {2}", req.SystemUser.FirstName, req.SystemUser.LastName, req.SystemUser.MotherName),
                Position = req.SystemUser.Post,
                UserAddress = req.UserRegister.DepartmentAddress != null ? req.UserRegister.DepartmentAddress.Address : "",
                WorkCloseDate = requestWorkKind.CloseDate.HasValue ? requestWorkKind.CloseDate.Value.ToShortDateString() : null,
            };
            return XmlSerializerHelper.Serialize(entity, true);
        }
    }
}